/**
 * Xinfei.com Inc.
 * Copyright (c) 2004-2023 All Rights Reserved.
 */
package com.xinfei.techplayprod.biz.controller;

import com.xinfei.techplayprod.biz.common.TechplayConstants;
import com.xinfei.techplayprod.biz.log.DigestLogAnnotated;
import com.xinfei.techplayprod.biz.log.TradeDigestLog;
import com.xinfei.techplayprod.biz.rr.TestResponse;
import com.xinfei.techplayprod.biz.rr.UserRequest;
import com.xinfei.techplayprod.biz.rr.UserResponse;
import com.xinfei.techplayprod.biz.service.UserService;
import com.xinfei.techplayprod.biz.template.TradeCallBack;
import com.xinfei.techplayprod.biz.template.TradeTemplate;
import com.xinfei.techplayprod.biz.util.ParamCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.xinfei.techplayprod.biz.util.ParamCheckUtil.*;

/**
 * 用户信息远程接口默认实现类<br/>
 *
 * @author Chengsheng.Li
 * @version $ UserFacadeImpl, v 0.1 2023/8/28 12:20 Chengsheng.Li Exp $
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/addOrEdit")
    @DigestLogAnnotated(TechplayConstants.TRADE_DIGEST_LOG)
    public UserResponse addOrEdit(@RequestBody UserRequest request) {

        final UserResponse result = new UserResponse();

        TradeTemplate.trade(result, new TradeCallBack() {

            public void checkParameter() {
                ParamCheckUtil.checkParamNotNull(request, USER_REQUEST);
                ParamCheckUtil.checkParamNotNull(request.getUser(), USER_REQUEST_USER);
                ParamCheckUtil.checkParamNotBlank(request.getUser().getName(), USER_NAME);
                ParamCheckUtil.checkParamNotBlank(request.getUser().getEmail(), USER_EMAIL);
            }

            @Override
            public void doTrade() {
                userService.addOrEdit(request.getUser());
                result.setId(request.getUser().getId());
            }

            public TradeDigestLog buildDigestLog() {
                return new TradeDigestLog(request);
            }

        });

        return result;
    }

    /*
     * 此接口仅仅是用来做性能测试使用
     * */
    @RequestMapping("/test")
    @DigestLogAnnotated(TechplayConstants.TRADE_DIGEST_LOG)
    public TestResponse test(@RequestParam("t") int t) {

        final TestResponse result = new TestResponse();

        TradeTemplate.trade(result, new TradeCallBack() {

            public void checkParameter() {

            }

            @Override
            public void doTrade() {
                result.setTime(userService.test(t));
            }

            public TradeDigestLog buildDigestLog() {
                return new TradeDigestLog("test");
            }

        });

        return result;
    }

    public void fn(int a, int b, int c,int d, int f, int g) {
        final int test=1;
    }
}
